import bb.cascades 1.3
import bb.WebImageView 1.4
import bb.data 1.0
import "controls"
import "lib"

Page {
    actionBarVisibility: ChromeVisibility.Overlay
    property string requestUrl
    onRequestUrlChanged: {
        console.log(requestUrl);
        dataSource.source = requestUrl;
    }
    
    
    function toPage(json){
        var items = json.body.subjects;
        var results = [];
        for (var i = 0; i < items.length; i ++) {
            var item = items[i];
            if(item.view == "multiTitle"){
                item["itemRoot"] = "0000"
                item["itemType"] = "multiTitle"
                item["index"] = "0000";
                item["screenWidth"] = com.screenWidth
                results.push(item)
            }else if(item.view == "text"){
                item["itemRoot"] = "0001"
                item["itemType"] = "text"
                item["index"] = "0001";
                item["screenWidth"] = com.screenWidth
                results.push(item)
            }else if(item.view == "slider"){
                item["itemRoot"] = "0002"
                item["itemType"] = "slider"
                item["index"] = "0002";
                item["screenWidth"] = com.screenWidth
                item["root"] = navRoot
                results.push(item)
            }else if(item.view == "list"){
                for (var j=0;j<item.podItems.length;j++){
                    var subItem = item.podItems[j];
                    subItem["itemRoot"] = "0003"
                    subItem["itemType"] = qsTr("%1_%2").arg("list").arg(subItem.style)
                    subItem["index"] = com.getIndex(i, j, 4);
                    subItem["screenWidth"] = com.screenWidth
                    results.push(subItem)
                }
            }
        }
        
        view.insertList(results)
    }
    
    onCreationCompleted: {
    }
    
    Container {
        layout: DockLayout {
        }
        Loader {
            id: loader
        }
        
        PullToRefreshListView {
            id: view
            
            onTriggered: {
                var currentItem = dataModel.data(indexPath);
                
                 var docCom = Qt.createComponent("asset:///ViewTopicDoc.qml")
                 var docView = docCom.createObject(navRoot);
                 docView.item = currentItem
                 navRoot.push(docView)
            }
            
            listItemComponents: [
                ListItemComponent {
                    type: "multiTitle"
                    CustomListItem {
                        TopicMultiTitle {
                            item: ListItemData
                        }
                    }
                },
                ListItemComponent {
                    type: "text"
                     Container {
                         Label {
                             text: ListItemData.content.intro
                             multiline: true
                         }
                     }
                },
                ListItemComponent {
                    type: "slider"
                    Container {
                        preferredWidth: ListItemData.screenWidth
                        preferredHeight: 360*(preferredWidth/640)
                        TopicSlider {
                            items: ListItemData.podItems
                            root: ListItemData.root
                        }
                    }
                },
                ListItemComponent {
                    type: "list_doc"
                    CustomListItem {
                        TopicDocItem {
                            width:  150*1.2
                            height: 106*1.2
                            title: ListItemData.title
                            screenWidth: ListItemData.screenWidth
                            preferredWidth: ListItemData.screenWidth
                            thumbnail: ListItemData.thumbnail
                            updateTime: ListItemData.updateTime
                            comments: ListItemData.commentCount
                            hasVideo: ListItemData.hasVideo
                        }
                    }
                }
            ]
        }
    }
    
    attachedObjects: [
        Common {
            id: com
        },
        
        DataSource {
            id: dataSource
            onSourceChanged: {
                loader.start();
                dataSource.load()
            }
            onDataLoaded: {
                toPage(data)
                loader.stop();
            }
            
            onError: {
                _Setting.alert(qsTr("Load Error."))
                loader.stop();
            }
        }
    ]
}
